﻿using App_Start;
using BLL;
using Common.Class;
using Common.Model;
using Microsoft.AspNetCore.Mvc;

namespace RdpServer.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RdpController : ControllerBase
    {
        /// <summary>
        /// 获取会话列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckLocalFilter]
        [CheckLoginFilter]
        public ServiceResultModel SessionListGet()
        {
            ServiceResultModel _result = new ServiceResultModel() { Success = 0 };

            try
            {
                if (SessionBLL.SessionList == null)
                {
                    SessionBLL.SessionListGet();
                }
                _result.Success = 1;
                List<SessionModel> _sessionList = new List<SessionModel>();
                _sessionList.Add(new SessionModel(SessionType.Directory)
                {
                    ID = 0,
                    Name = "服务器会话",
                    ChildSessionList = SessionBLL.SessionList
                });
                _result.Result = _sessionList;

            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(CommonHelper.GetNewID(1));
                LogHelper.WriteLogToFile("Error", _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "");
                return new ServiceResultModel
                {
                    Success = -1,
                    ErrorMessage = "错误代码:" + _errorID
                };
            }

            return _result;
        }

        /// <summary>
        /// 添加会话
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckLocalFilter]
        [CheckLoginFilter]
        public ServiceResultModel SessionAdd()
        {
            ServiceResultModel _result = new ServiceResultModel() { Success = 0 };

            try
            {
                if (SessionBLL.SessionList == null)
                {
                    SessionBLL.SessionListGet();
                }

                string _paramStr = Convert.ToString(Request.Form["ParamStr"]);
                JsonHelper _jsonParam = new JsonHelper(_paramStr);
                string _fSessionID = _jsonParam.GetVar("FSessionID");
                string _sessionName = _jsonParam.GetVar("SessionName");
                string _sessionServer = _jsonParam.GetVar("Server");
                string _port = _jsonParam.GetVar("Port");
                string _userName = _jsonParam.GetVar("UserName");
                string _password = _jsonParam.GetVar("Password");
                string _isShareDisk = _jsonParam.GetVar("IsShareDisk");
                string _description = _jsonParam.GetVar("Description");
                if (string.IsNullOrWhiteSpace(_sessionName))
                {
                    _result.ErrorMessage = "服务名称不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_sessionServer))
                {
                    _result.ErrorMessage = "服务器地址不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_port))
                {
                    _result.ErrorMessage = "服务端口不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_userName))
                {
                    _result.ErrorMessage = "用户名不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_password))
                {
                    _result.ErrorMessage = "密码不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_isShareDisk))
                {
                    _isShareDisk = "N";
                }

                SessionModel _session = new SessionModel(SessionType.SessionRdpClient);
                RdpSessionModel RdpSession = new RdpSessionModel();
                _session.ID = CommonHelper.GetNewID();
                _session.Data = RdpSession;
                _session.Name = _sessionName.Trim();
                RdpSession.Server = _sessionServer.Trim();
                RdpSession.Port = Convert.ToInt32(_port.Trim());
                RdpSession.UserName = _userName.Trim();
                RdpSession.Password = _password.Trim();
                RdpSession.IsShareDisk = _isShareDisk.Trim() == "Y" ? "Y" : "N";
                RdpSession.Description = _description.Trim();

                if (_fSessionID == "0")
                {

                    SessionBLL.SessionList.Add(_session);
                    SessionBLL.SessionList = SessionBLL.SessionList.OrderBy(it => it.Name).ToList();
                    SessionBLL.SessionListSave(SessionBLL.SessionList);
                    _result.Success = 1;
                    List<SessionModel> _sessionList = new List<SessionModel>();
                    _sessionList.Add(new SessionModel(SessionType.Directory)
                    {
                        ID = 0,
                        Name = "服务器会话",
                        ChildSessionList = SessionBLL.SessionList
                    });
                    _result.Result = _sessionList;
                    return _result;

                }
                else
                {
                    var _re = SessionBLL.SessionGetFromID(SessionBLL.SessionList, Convert.ToInt64(_fSessionID));
                    if (_re.session != null)
                    {
                        if(_re.session.SessionType==SessionType.SessionRdpClient)
                        {
                            _re.fSession.ChildSessionList.Add(_session);
                            _re.fSession.ChildSessionList = _re.fSession.ChildSessionList.OrderBy(it => it.Name).ToList();
                        }
                        else
                        {
                            _re.session.ChildSessionList.Add(_session);
                            _re.session.ChildSessionList = _re.session.ChildSessionList.OrderBy(it => it.Name).ToList();
                        } 
                        SessionBLL.SessionListSave(SessionBLL.SessionList);
                        _result.Success = 1;
                        List<SessionModel> _sessionList = new List<SessionModel>();
                        _sessionList.Add(new SessionModel(SessionType.Directory)
                        {
                            ID = 0,
                            Name = "服务器会话",
                            ChildSessionList = SessionBLL.SessionList
                        });
                        _result.Result = _sessionList;
                        return _result;
                    }

                }



                _result.ErrorMessage = "未找到会话";

            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(CommonHelper.GetNewID(1));
                LogHelper.WriteLogToFile("Error", _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "");
                return new ServiceResultModel
                {
                    Success = -1,
                    ErrorMessage = "错误代码:" + _errorID
                };
            }

            return _result;
        }

        /// <summary>
        /// 更新会话
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckLocalFilter]
        [CheckLoginFilter]
        public ServiceResultModel SessionUpdate()
        {
            ServiceResultModel _result = new ServiceResultModel() { Success = 0 };

            try
            {
                if (SessionBLL.SessionList == null)
                {
                    SessionBLL.SessionListGet();
                }

                string _paramStr = Convert.ToString(Request.Form["ParamStr"]);
                JsonHelper _jsonParam = new JsonHelper(_paramStr);
                string _sessionID = _jsonParam.GetVar("SessionID");
                string _sessionName = _jsonParam.GetVar("SessionName");
                string _sessionServer = _jsonParam.GetVar("Server");
                string _port = _jsonParam.GetVar("Port");
                string _userName = _jsonParam.GetVar("UserName");
                string _password = _jsonParam.GetVar("Password");
                string _isShareDisk = _jsonParam.GetVar("IsShareDisk");
                string _description = _jsonParam.GetVar("Description");

                if (string.IsNullOrWhiteSpace(_sessionName))
                {
                    _result.ErrorMessage = "服务名称不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_sessionServer))
                {
                    _result.ErrorMessage = "服务器地址不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_port))
                {
                    _result.ErrorMessage = "服务端口不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_userName))
                {
                    _result.ErrorMessage = "用户名不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_password))
                {
                    _result.ErrorMessage = "密码不可为空";
                    return _result;
                }
                if (string.IsNullOrWhiteSpace(_isShareDisk))
                {
                    _isShareDisk = "N";
                }

                var _re = SessionBLL.SessionGetFromID(SessionBLL.SessionList, Convert.ToInt64(_sessionID));
                if (_re.session != null)
                {
                    SessionModel _session = _re.session;
                    RdpSessionModel RdpSession = new RdpSessionModel();
                    _session.Data = RdpSession;
                    _session.Name = _sessionName.Trim();
                    RdpSession.Server = _sessionServer.Trim();
                    RdpSession.Port = Convert.ToInt32(_port.Trim());
                    RdpSession.UserName = _userName.Trim();
                    RdpSession.Password = _password.Trim();
                    RdpSession.IsShareDisk = _isShareDisk.Trim() == "Y" ? "Y" : "N";
                    RdpSession.Description = _description.Trim();
                    if (_re.fSession != null)
                    {
                        _re.fSession.ChildSessionList = _re.fSession.ChildSessionList.OrderBy(it => it.Name).ToList();
                    }
                    SessionBLL.SessionListSave(SessionBLL.SessionList);
                    _result.Success = 1;
                    List<SessionModel> _sessionList = new List<SessionModel>();
                    _sessionList.Add(new SessionModel(SessionType.Directory)
                    {
                        ID = 0,
                        Name = "服务器会话",
                        ChildSessionList = SessionBLL.SessionList
                    });
                    _result.Result = _sessionList;
                    return _result;
                }

                _result.ErrorMessage = "未找到会话";

            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(CommonHelper.GetNewID(1));
                LogHelper.WriteLogToFile("Error", _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "");
                return new ServiceResultModel
                {
                    Success = -1,
                    ErrorMessage = "错误代码:" + _errorID
                };
            }

            return _result;
        }

        /// <summary>
        /// 删除会话
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckLocalFilter]
        [CheckLoginFilter]
        public ServiceResultModel SessionDelete()
        {
            ServiceResultModel _result = new ServiceResultModel() { Success = 0 };

            try
            {
                if (SessionBLL.SessionList == null)
                {
                    SessionBLL.SessionListGet();
                }

                string _paramStr = Convert.ToString(Request.Form["ParamStr"]);
                JsonHelper _jsonParam = new JsonHelper(_paramStr);
                string _sessionID = _jsonParam.GetVar("SessionID");

                if (string.IsNullOrWhiteSpace(_sessionID))
                {
                    _result.ErrorMessage = "会话ID不可为空";
                    return _result;
                }

                var _re = SessionBLL.SessionGetFromID(SessionBLL.SessionList, Convert.ToInt64(_sessionID));
                if (_re.session != null)
                {
                    if (_re.fSession != null)
                    {
                        _re.fSession.ChildSessionList.Remove(_re.session);
                    }
                    else
                    {
                        SessionBLL.SessionList.Remove(_re.session);
                    }
                    SessionBLL.SessionListSave(SessionBLL.SessionList);
                    _result.Success = 1;
                    List<SessionModel> _sessionList = new List<SessionModel>();
                    _sessionList.Add(new SessionModel(SessionType.Directory)
                    {
                        ID = 0,
                        Name = "服务器会话",
                        ChildSessionList = SessionBLL.SessionList
                    });
                    _result.Result = _sessionList;
                    return _result;
                }

                _result.ErrorMessage = "未找到会话";

            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(CommonHelper.GetNewID(1));
                LogHelper.WriteLogToFile("Error", _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "");
                return new ServiceResultModel
                {
                    Success = -1,
                    ErrorMessage = "错误代码:" + _errorID
                };
            }

            return _result;
        }


        /// <summary>
        /// 添加文件夹
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckLocalFilter]
        [CheckLoginFilter]
        public ServiceResultModel DirAdd()
        {
            ServiceResultModel _result = new ServiceResultModel() { Success = 0 };

            try
            {
                if (SessionBLL.SessionList == null)
                {
                    SessionBLL.SessionListGet();
                }

                string _paramStr = Convert.ToString(Request.Form["ParamStr"]);
                JsonHelper _jsonParam = new JsonHelper(_paramStr);
                string _fSessionID = _jsonParam.GetVar("FSessionID");
                string _sessionName = _jsonParam.GetVar("SessionName");

                if (string.IsNullOrWhiteSpace(_sessionName))
                {
                    _result.ErrorMessage = "文件夹名称不可为空";
                    return _result;
                }

                SessionModel _session = new SessionModel(SessionType.Directory);
                _session.ID = CommonHelper.GetNewID();
                _session.Name = _sessionName.Trim();

                if (_fSessionID == "0")
                {
                    SessionBLL.SessionList.Add(_session);
                    SessionBLL.SessionList = SessionBLL.SessionList.OrderBy(it => it.Name).ToList();
                    SessionBLL.SessionListSave(SessionBLL.SessionList);
                    _result.Success = 1;
                    List<SessionModel> _sessionList = new List<SessionModel>();
                    _sessionList.Add(new SessionModel(SessionType.Directory)
                    {
                        ID = 0,
                        Name = "服务器会话",
                        ChildSessionList = SessionBLL.SessionList
                    });
                    _result.Result = _sessionList;
                    return _result;
                }
                else
                {
                    var _re = SessionBLL.SessionGetFromID(SessionBLL.SessionList, Convert.ToInt64(_fSessionID));
                    if (_re.session != null)
                    {
                        _re.session.ChildSessionList.Add(_session);
                        _re.session.ChildSessionList = _re.session.ChildSessionList.OrderBy(it => it.Name).ToList();
                        SessionBLL.SessionListSave(SessionBLL.SessionList);
                        _result.Success = 1;
                        List<SessionModel> _sessionList = new List<SessionModel>();
                        _sessionList.Add(new SessionModel(SessionType.Directory)
                        {
                            ID = 0,
                            Name = "服务器会话",
                            ChildSessionList = SessionBLL.SessionList
                        });
                        _result.Result = _sessionList;
                        return _result;
                    }
                }


                _result.ErrorMessage = "未找到会话";

            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(CommonHelper.GetNewID(1));
                LogHelper.WriteLogToFile("Error", _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "");
                return new ServiceResultModel
                {
                    Success = -1,
                    ErrorMessage = "错误代码:" + _errorID
                };
            }

            return _result;
        }

        /// <summary>
        /// 更新文件夹
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [CheckLocalFilter]
        [CheckLoginFilter]
        public ServiceResultModel DirUpdate()
        {
            ServiceResultModel _result = new ServiceResultModel() { Success = 0 };

            try
            {
                if (SessionBLL.SessionList == null)
                {
                    SessionBLL.SessionListGet();
                }

                string _paramStr = Convert.ToString(Request.Form["ParamStr"]);
                JsonHelper _jsonParam = new JsonHelper(_paramStr);
                string _sessionID = _jsonParam.GetVar("SessionID");
                string _sessionName = _jsonParam.GetVar("SessionName");

                if (string.IsNullOrWhiteSpace(_sessionName))
                {
                    _result.ErrorMessage = "服务名称不可为空";
                    return _result;
                }


                var _re = SessionBLL.SessionGetFromID(SessionBLL.SessionList, Convert.ToInt64(_sessionID));
                if (_re.session != null)
                {
                    SessionModel _session = _re.session;
                    _session.Name = _sessionName.Trim();
                    if (_re.fSession != null)
                    {
                        _re.fSession.ChildSessionList = _re.fSession.ChildSessionList.OrderBy(it => it.Name).ToList();
                    }
                    SessionBLL.SessionListSave(SessionBLL.SessionList);
                    _result.Success = 1;
                    List<SessionModel> _sessionList = new List<SessionModel>();
                    _sessionList.Add(new SessionModel(SessionType.Directory)
                    {
                        ID = 0,
                        Name = "服务器会话",
                        ChildSessionList = SessionBLL.SessionList
                    });
                    _result.Result = _sessionList;
                    return _result;
                }

                _result.ErrorMessage = "未找到会话";

            }
            catch (Exception ex)
            {
                string _errorID = Convert.ToString(CommonHelper.GetNewID(1));
                LogHelper.WriteLogToFile("Error", _errorID + "---" + ex.Message + "\r\n" + ex.StackTrace, "");
                return new ServiceResultModel
                {
                    Success = -1,
                    ErrorMessage = "错误代码:" + _errorID
                };
            }

            return _result;
        }


    }
}
